'use strict';

const express = require('express');
const dbutility = require('../../common/dbutility');
const gputility = require('../../common/gputility');
const rdsutility = require('../../common/rdsutility');
const logger = require('../../common/logutility').logger('updExpConf');
let router = express.Router();

router.get('/', function(req, res) {
    gputility.excuteGP(getGP(req, res));
});

router.post('/', function(req, res) {
    gputility.excuteGP(postGP(req, res));
});

function* getGP(req, res) {
    try {
        res.render('./cou/updExpConf', req.session.expUpd);
    } catch (e) {
        logger.error(e);
        res.redirect('/error');
    }
}

function* postGP(req, res) {
    try {
        let expNo = req.session.expUpd.expNo;
        let expReName = req.session.expUpd.expReName;
        let expReTel = req.session.expUpd.expReTel;
        let expPay = req.session.expUpd.expPay;
        let expCouTel = req.session.userTel;
        yield dbutility.expModel.updateExp(expNo, expCouTel, expReName, expReTel, expPay)
        yield rdsutility.addReTelName(expReTel, expReName);

        logger.info('Update Exp Info:');
        logger.info(req.session.expUpd);

        res.redirect('/updExpCmp');
    } catch (e) {
        logger.error(e);
        res.redirect('/error');
    }
}

module.exports = router;